<!DOCTYPE html>


<html lang="en">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    JavaScript数据结构和算法 |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
      <canvas width="1777" height="841"
        style="position: fixed; left: 0px; top: 0px; z-index: 99999; pointer-events: none;"></canvas>
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-数据结构和算法"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  JavaScript数据结构和算法
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/24/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/" class="article-date">
  <time datetime="2020-11-24T10:16:00.000Z" itemprop="datePublished">2020-11-24</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/">数据结构和算法</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">4.6k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">21 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="数据结构"><a href="#数据结构" class="headerlink" title="数据结构"></a>数据结构</h2><blockquote>
<p>存储和组织数据的一种方式</p>
</blockquote>
<h3 id="数组（Array）"><a href="#数组（Array）" class="headerlink" title="数组（Array）"></a>数组（Array）</h3><blockquote>
<p>通常情况下用于存储一系列同一种数据类型的值</p>
</blockquote>
<h4 id="创建和初始化"><a href="#创建和初始化" class="headerlink" title="创建和初始化"></a>创建和初始化</h4><ul>
<li><code>new Array(&#39;a&#39;,&#39;b&#39;)</code></li>
<li><code>[&#39;a&#39;,&#39;b&#39;]</code></li>
</ul>
<h4 id="常用操作"><a href="#常用操作" class="headerlink" title="常用操作"></a>常用操作</h4><h5 id="添加元素"><a href="#添加元素" class="headerlink" title="添加元素"></a>添加元素</h5><ul>
<li>数组的末尾添加元素 <code>arr.push(item)</code></li>
<li>数组的首位插入一个元素 <code>arr.unshift(item)</code></li>
<li>指定位置插入元素 <code>arr.splice(index,0,item)</code></li>
</ul>
<h5 id="删除元素"><a href="#删除元素" class="headerlink" title="删除元素"></a>删除元素</h5><ul>
<li>数组的末尾删除一个元素 <code>arr.pop()</code></li>
<li>数组的首位删除一个元素 <code>arr.shift()</code></li>
<li>删除指定位置的元素 <code>arr.splice(start,number)</code>   </li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">arr.splice(<span class="number">1</span>,<span class="number">2</span>) <span class="comment">// 起始位置，删除的个数</span></span><br><span class="line">arr <span class="comment">// [1,4]</span></span><br></pre></td></tr></table></figure>
<h5 id="修改元素"><a href="#修改元素" class="headerlink" title="修改元素"></a>修改元素</h5><ul>
<li>修改指定索引的元素 <code>arr.splice(index,1,item)</code></li>
<li>修改指定索引的多个元素 <code>arr.splice(index,number,item)</code>  </li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">arr.splice(<span class="number">1</span>,<span class="number">2</span>,<span class="string">&#x27;qq&#x27;</span>,<span class="string">&#x27;bb&#x27;</span>)</span><br><span class="line">arr <span class="comment">// [1,&#x27;qq&#x27;,&#x27;bb&#x27;,4,5]</span></span><br></pre></td></tr></table></figure>

<h3 id="栈（Stack）"><a href="#栈（Stack）" class="headerlink" title="栈（Stack）"></a>栈（Stack）</h3><blockquote>
<p>数组是一个线性结构，并且可以在数组的任意位置插入和删除元素。但是有时候，我们为了实现某些功能，必须对这种任意性加以限制。栈和队列就是比较常见的受限的线性结构。</p>
</blockquote>
<p>栈（stack）是一种运算受限的线性表：</p>
<ul>
<li><code>LIFO（last in first out）</code>表示就是后进入的元素，第一个弹出栈空间</li>
<li>其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶，相对地，把另一端称为栈底</li>
<li>向一个栈插入新元素又称作进栈(入栈/压栈)，它是把新元素放到栈顶元素的上面，使之成为新的栈顶元素</li>
<li>从一个栈删除元素又称作出栈(退栈)，它是把栈顶元素删除掉，使其相邻的元素成为新的栈顶元素</li>
</ul>
<p><img src="https://user-images.githubusercontent.com/24516169/88035463-caf63780-cb74-11ea-910d-e396a83659ea.png"><br><code>先进后出，后进先出</code></p>
<h4 id="程序中的栈结构"><a href="#程序中的栈结构" class="headerlink" title="程序中的栈结构"></a>程序中的栈结构</h4><ul>
<li><p><code>函数调用</code>: <code>A(B(C(D())))</code>: 即 A 函数中调用 B，B 调用 C，C 调用 D；在 A 执行的过程中会将 A 压入栈，随后 B 执行时 B 也被压入栈，函数 C 和 D 执行时也会被压入栈。所以当前栈的顺序为：A-&gt;B-&gt;C-&gt;D（栈顶）；函数 D 执行完之后，会弹出栈被释放，弹出栈的顺序为 D-&gt;C-&gt;B-&gt;A</p>
</li>
<li><p><code>递归</code>: 为什么没有停止条件的递归会造成栈溢出？比如函数 A 为递归函数，不断地调用自己（因为函数还没有执行完，不会把函数弹出栈），不停地把相同的函数 A 压入栈，最后造成栈溢出(Queue Overfloat)</p>
</li>
</ul>
<h4 id="栈结构实现"><a href="#栈结构实现" class="headerlink" title="栈结构实现"></a>栈结构实现</h4><h5 id="常用操作-1"><a href="#常用操作-1" class="headerlink" title="常用操作"></a>常用操作</h5><ul>
<li><code>push(item)</code> 添加一个新元素到栈顶</li>
<li><code>pop()</code> 移除栈顶元素（出栈）</li>
<li><code>peek()</code> 返回栈顶的元素，不对栈做任何修改（该方法不会移除栈顶的元素，仅仅返回它）</li>
<li><code>isEmpty()</code> 判断栈内是否有元素</li>
<li><code>size()</code> 返回栈内元素个数</li>
<li><code>toString()</code> 将栈结构的内容通过字符串的形式返回</li>
</ul>
<h5 id="JavaScript实现栈"><a href="#JavaScript实现栈" class="headerlink" title="JavaScript实现栈"></a>JavaScript实现栈</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Stock</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = []</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 进栈</span></span><br><span class="line">    push(item) &#123;</span><br><span class="line">        <span class="built_in">this</span>.items.push(item)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 出栈,并返回该元素</span></span><br><span class="line">    pop() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.pop()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 返回当前栈顶元素(不改变栈)</span></span><br><span class="line">    peek() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items[<span class="built_in">this</span>.items.length - <span class="number">1</span>]</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 判断栈是否为空</span></span><br><span class="line">    isEmpty() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.length ? <span class="literal">false</span> : <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取栈元素的个数</span></span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.length</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 以字符串的形式返回栈内元素</span></span><br><span class="line">    toString() &#123;</span><br><span class="line">        <span class="keyword">let</span> result = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="built_in">this</span>.items.forEach(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">            result += item + <span class="string">&#x27;&#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> stock = <span class="keyword">new</span> Stock()</span><br><span class="line">stock.push(<span class="number">1</span>)</span><br><span class="line">stock.push(<span class="number">1</span>)</span><br><span class="line">stock.push(<span class="number">2</span>)</span><br><span class="line"><span class="built_in">console</span>.log(stock.pop()) <span class="comment">// 2</span></span><br><span class="line"><span class="built_in">console</span>.log(stock.items) <span class="comment">// [1,1]</span></span><br><span class="line"><span class="built_in">console</span>.log(stock.peek()) <span class="comment">// 1</span></span><br><span class="line"><span class="built_in">console</span>.log(stock.isEmpty()) <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(stock.size()) <span class="comment">// 2</span></span><br><span class="line"><span class="built_in">console</span>.log(stock.toString()) <span class="comment">// 1 1</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h5 id="简单应用"><a href="#简单应用" class="headerlink" title="简单应用"></a>简单应用</h5><blockquote>
<p>栈结构实现十进制转二进制</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">dec2bin</span>(<span class="params">number</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> stock = <span class="keyword">new</span> Stock()</span><br><span class="line">    <span class="keyword">while</span> (number) &#123;</span><br><span class="line">        stock.push(number % <span class="number">2</span>)</span><br><span class="line">        number = <span class="built_in">Math</span>.floor(number / <span class="number">2</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">    <span class="keyword">while</span>(!stock.isEmpty())&#123;</span><br><span class="line">        str += stock.pop()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> str</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(dec2bin(<span class="number">10</span>))  <span class="comment">// 1010</span></span><br><span class="line"><span class="built_in">console</span>.log((<span class="number">10</span>).toString(<span class="number">2</span>)) <span class="comment">//1010</span></span><br></pre></td></tr></table></figure>
<h3 id="队列（Queue）"><a href="#队列（Queue）" class="headerlink" title="队列（Queue）"></a>队列（Queue）</h3><blockquote>
<p>是一种运算受限的线性表，特点：<code>先进先出</code>。(FIFO：First In First Out)</p>
</blockquote>
<p><img src="https://user-images.githubusercontent.com/24516169/88038782-45c15180-cb79-11ea-8439-bdc7e240d10d.png"></p>
<h4 id="常用操作-2"><a href="#常用操作-2" class="headerlink" title="常用操作"></a>常用操作</h4><ul>
<li><code>enqueue(element)</code> 向队尾添加一个/多个新的项</li>
<li><code>dequeue()</code> 移除队列的第一项，并返回移除的元素</li>
<li><code>front()</code> 返回队列中的第一个元素（最先被添加的，也是最早被移除的元素。不改变队列）</li>
<li><code>isEmpty()</code> 是否有元素</li>
<li><code>size()</code> 返回队列元素个数</li>
<li><code>toString()</code> 将队列内容转换成字符串的形式</li>
</ul>
<h4 id="JavaScript实现队列"><a href="#JavaScript实现队列" class="headerlink" title="JavaScript实现队列"></a>JavaScript实现队列</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Queue</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = []</span><br><span class="line">    &#125;</span><br><span class="line">    enqueue(element) &#123;</span><br><span class="line">        <span class="built_in">this</span>.items.push(element)</span><br><span class="line">    &#125;</span><br><span class="line">    dequeue() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.shift()</span><br><span class="line">    &#125;</span><br><span class="line">    front() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items[<span class="number">0</span>]</span><br><span class="line">    &#125;</span><br><span class="line">    isEmpty() &#123;</span><br><span class="line">        <span class="keyword">return</span> !<span class="built_in">Boolean</span>(<span class="built_in">this</span>.items.length)</span><br><span class="line">    &#125;</span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.length</span><br><span class="line">    &#125;</span><br><span class="line">    toString() &#123;</span><br><span class="line">        <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="built_in">this</span>.items.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">            str += item + <span class="string">&#x27; &#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> queue = <span class="keyword">new</span> Queue</span><br><span class="line">queue.enqueue(<span class="number">1</span>)</span><br><span class="line">queue.enqueue(<span class="number">2</span>)</span><br><span class="line">queue.enqueue(<span class="number">3</span>)</span><br><span class="line"><span class="built_in">console</span>.log(queue.dequeue()) <span class="comment">// 1</span></span><br><span class="line"><span class="built_in">console</span>.log(queue.front())   <span class="comment">// 2</span></span><br><span class="line"><span class="built_in">console</span>.log(queue.isEmpty()) <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(queue.size())    <span class="comment">// 2</span></span><br><span class="line"><span class="built_in">console</span>.log(queue.toString()) <span class="comment">//  2 3</span></span><br></pre></td></tr></table></figure>

<h4 id="简单实现"><a href="#简单实现" class="headerlink" title="简单实现"></a>简单实现</h4><blockquote>
<p>击鼓传花游戏</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">game</span>(<span class="params">array, number</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> queue = <span class="keyword">new</span> Queue()</span><br><span class="line">    array.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">// 进入队列</span></span><br><span class="line">        queue.enqueue(item)</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (queue.size() &gt; <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; number - <span class="number">1</span>; i++) &#123;</span><br><span class="line">            <span class="comment">// 不是number项的进入队尾</span></span><br><span class="line">            queue.enqueue(queue.dequeue())</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 删除number项</span></span><br><span class="line">        queue.dequeue()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取留到最后一个的值</span></span><br><span class="line">    <span class="keyword">const</span> end = queue.front()</span><br><span class="line">    <span class="keyword">return</span> array.indexOf(end)  <span class="comment">// 返回值所在的索引</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> arr = [<span class="string">&#x27;doreen&#x27;</span>, <span class="string">&#x27;jerry&#x27;</span>, <span class="string">&#x27;sherry&#x27;</span>, <span class="string">&#x27;tom&#x27;</span>, <span class="string">&#x27;amy&#x27;</span>, <span class="string">&#x27;gaga&#x27;</span>]</span><br><span class="line"><span class="built_in">console</span>.log(arr[game(arr, <span class="number">2</span>)])  <span class="comment">// amy</span></span><br></pre></td></tr></table></figure>

<h4 id="优先队列"><a href="#优先队列" class="headerlink" title="优先队列"></a>优先队列</h4><!-- ![](https://user-images.githubusercontent.com/24516169/88051118-b02ebd80-cb8a-11ea-9acf-4329cbbff6fc.png) -->
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">QueueElement</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 元素和他的优先级</span></span><br><span class="line">    <span class="keyword">constructor</span>(element, priority) &#123;</span><br><span class="line">        <span class="built_in">this</span>.element = element</span><br><span class="line">        <span class="built_in">this</span>.priority = priority</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PriorityQueue</span> <span class="keyword">extends</span> <span class="title">Queue</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    enqueue(element, priority) &#123;</span><br><span class="line">        <span class="keyword">const</span> queueElement = <span class="keyword">new</span> QueueElement(element, priority)</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.isEmpty()) &#123;</span><br><span class="line">            <span class="built_in">this</span>.items.push(queueElement)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> add = <span class="literal">false</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="built_in">this</span>.items.length; i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="built_in">this</span>.items[i].priority &gt; queueElement.priority) &#123;</span><br><span class="line">                    <span class="built_in">this</span>.items.splice(i, <span class="number">0</span>, queueElement)</span><br><span class="line">                    add = <span class="literal">true</span>; <span class="keyword">break</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (!add) &#123;</span><br><span class="line">                <span class="built_in">this</span>.items.push(queueElement)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    toString() &#123;</span><br><span class="line">        <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="built_in">this</span>.items.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">            str += item.element + <span class="string">&#x27;-&#x27;</span> + item.priority + <span class="string">&#x27; &#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> queue = <span class="keyword">new</span> PriorityQueue()</span><br><span class="line">queue.enqueue(<span class="number">1</span>, <span class="number">1</span>)</span><br><span class="line">queue.enqueue(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"><span class="built_in">console</span>.log(queue.toString())   <span class="comment">// 2--0 1-1</span></span><br></pre></td></tr></table></figure>

<h3 id="链表（Linked-List）"><a href="#链表（Linked-List）" class="headerlink" title="链表（Linked List）"></a>链表（Linked List）</h3><h4 id="链表和数组"><a href="#链表和数组" class="headerlink" title="链表和数组"></a>链表和数组</h4><h5 id="数组缺点"><a href="#数组缺点" class="headerlink" title="数组缺点"></a>数组缺点</h5><ul>
<li><p>数组的创建需要申请一段连续的内存空间(一整块内存)，并且大小是固定的，当前数组不能满足容量需求时，需要扩容。 (一般情况下是申请一个更大的数组，比如 2 倍，然后将原数组中的元素复制过去)</p>
</li>
<li><p>在数组开头或中间位置插入数据的成本很高，需要进行大量元素的位移。</p>
</li>
</ul>
<h5 id="链表"><a href="#链表" class="headerlink" title="链表"></a>链表</h5><blockquote>
<p>链表中的元素在内存中不必是连续的空间。链表的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(有些语言称为指针)组成。</p>
</blockquote>
<h4 id="单向链表"><a href="#单向链表" class="headerlink" title="单向链表"></a>单向链表</h4><h6 id="优点"><a href="#优点" class="headerlink" title="优点"></a>优点</h6><ul>
<li>内存空间不必是连续的，可以充分利用计算机的内存，实现灵活的内存动态管理。</li>
<li>链表在插入和删除数据时，时间复杂度可以达到 O(1)，相对数组效率高很多。<h6 id="缺点"><a href="#缺点" class="headerlink" title="缺点"></a>缺点</h6></li>
<li>访问任何一个位置的元素时，需要从头开始访问。(无法跳过第一个元素访问任何一个元素)</li>
<li>无法通过下标值直接访问元素，需要从头开始一个个访问，直到找到对应的元素。</li>
<li>虽然可以轻松地到达下一个节点，但是回到前一个节点是很难的。</li>
</ul>
<h5 id="常用操作-3"><a href="#常用操作-3" class="headerlink" title="常用操作"></a>常用操作</h5><ul>
<li><code>append(element)</code> 链尾添加新项</li>
<li><code>insert(position,element)</code> 在指定位置添加新项</li>
<li><code>get(position)</code> 获取对应位置的元素</li>
<li><code>indexOf(element)</code> 返回该元素在链表的索引，没有则为-1</li>
<li><code>update(position,element)</code> 修改指定位置的元素</li>
<li><code>removeAt(position)</code> 从链表的特定位置移除一项</li>
<li><code>remove(element)</code> 从链表中移除一项</li>
<li><code>isEmpty()</code></li>
<li><code>size()</code></li>
<li><code>toString()</code></li>
</ul>
<h5 id="JavaScript实现单向链表"><a href="#JavaScript实现单向链表" class="headerlink" title="JavaScript实现单向链表"></a>JavaScript实现单向链表</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(data) &#123;</span><br><span class="line">        <span class="built_in">this</span>.data = data</span><br><span class="line">        <span class="built_in">this</span>.next = <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkedList</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.length = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.head = <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">    apped(data) &#123;</span><br><span class="line">        <span class="keyword">let</span> node = <span class="keyword">new</span> Node(data)</span><br><span class="line">        <span class="keyword">if</span> (!<span class="built_in">this</span>.length) &#123;</span><br><span class="line">            <span class="built_in">this</span>.head = node</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> nextNode = <span class="built_in">this</span>.head</span><br><span class="line">            <span class="keyword">while</span> (nextNode.next !== <span class="literal">null</span>) &#123;</span><br><span class="line">                nextNode = nextNode.next</span><br><span class="line">            &#125;</span><br><span class="line">            nextNode.next = node</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.length++</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    insert(position, element) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.length &gt;= position &amp;&amp; position &gt;= <span class="number">0</span>) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">let</span> newNode = <span class="keyword">new</span> Node(element)</span><br><span class="line">            <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">            <span class="keyword">let</span> nextNode;</span><br><span class="line">            <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="built_in">this</span>.head = newNode</span><br><span class="line">                <span class="built_in">this</span>.head.next = currentNode</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">while</span> (position &gt; <span class="number">1</span>) &#123;</span><br><span class="line">                    currentNode = currentNode.next</span><br><span class="line">                    position--</span><br><span class="line">                &#125;</span><br><span class="line">                nextNode = currentNode.next</span><br><span class="line">                currentNode.next = newNode</span><br><span class="line">                newNode.next = nextNode</span><br><span class="line">                <span class="built_in">this</span>.length++</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.head</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    get(position) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt;= <span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) <span class="keyword">return</span> <span class="built_in">this</span>.head.data</span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            position--</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> currentNode.data</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    indexOf(element) &#123;</span><br><span class="line">        <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">while</span> (currentNode) &#123;</span><br><span class="line">            <span class="keyword">if</span> (currentNode.data === element) &#123;</span><br><span class="line">                <span class="keyword">return</span> index</span><br><span class="line">            &#125;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            index++</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    update(position, element) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &gt;= <span class="built_in">this</span>.length || position &lt; <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">let</span> newNode = <span class="keyword">new</span> Node(element)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            position--</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        currentNode.data = newNode.data</span><br><span class="line">        <span class="keyword">return</span> currentNode</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    removeAt(position) &#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (position &gt;= <span class="built_in">this</span>.length || position &lt; <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">this</span>.head = <span class="built_in">this</span>.head.next</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> previousNode;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                previousNode = currentNode</span><br><span class="line">                currentNode = currentNode.next</span><br><span class="line">                position--</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">            previousNode.next = currentNode.next</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">this</span>.length--</span><br><span class="line">        <span class="keyword">return</span> currentNode</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    remove(element) &#123;</span><br><span class="line">        <span class="built_in">this</span>.removeAt(<span class="built_in">this</span>.indexOf(element))</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    isEmpty() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length === <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    toString() &#123;</span><br><span class="line">        <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">while</span> (currentNode) &#123;</span><br><span class="line">            str += currentNode.data + <span class="string">&#x27; &#x27;</span></span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> link = <span class="keyword">new</span> LinkedList()</span><br><span class="line">link.apped(<span class="number">2</span>)</span><br><span class="line">link.apped(<span class="number">5</span>)</span><br><span class="line">link.insert(<span class="number">2</span>, <span class="number">4</span>)   <span class="comment">// 2 -&gt; 5 -&gt; 4</span></span><br><span class="line"><span class="built_in">console</span>.log(link.get(<span class="number">1</span>))    <span class="comment">// 5</span></span><br><span class="line"><span class="built_in">console</span>.log(link.indexOf(<span class="number">5</span>))    <span class="comment">// 1</span></span><br><span class="line"><span class="built_in">console</span>.log(link.removeAt(<span class="number">1</span>))   <span class="comment">// 删掉了：5  链表：2 -&gt; 4</span></span><br><span class="line">link.remove(<span class="number">2</span>)</span><br><span class="line"><span class="built_in">console</span>.log(link.toString())    <span class="comment">// 4</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="双向链表"><a href="#双向链表" class="headerlink" title="双向链表"></a>双向链表</h4><ul>
<li>既可以从头遍历到尾，也可以从尾遍历到头。</li>
<li>链表相连的过程是双向的。实现原理是一个节点既有向前连接的引用，也有一个向后连接的引用。</li>
<li>双向链表可以有效的解决单向链表存在的问题。</li>
<li>双向链表缺点：<ul>
<li>每次在插入或删除某个节点时，都需要处理四个引用，而不是两个，实现起来会困难些。</li>
<li>相对于单向链表，所占内存空间更大一些。<h5 id="双向链表结构"><a href="#双向链表结构" class="headerlink" title="双向链表结构"></a>双向链表结构</h5><img src="https://user-images.githubusercontent.com/24516169/88497604-724ef080-cff3-11ea-969b-3496e3a64ca6.png"><h5 id="常用操作-4"><a href="#常用操作-4" class="headerlink" title="常用操作"></a>常用操作</h5></li>
</ul>
</li>
</ul>
<ul>
<li><code>append(element)</code> 向链表尾部追加一个新元素。</li>
<li><code>insert(position, element)</code> 向链表的指定位置插入一个新元素。</li>
<li><code>get(position)</code> 获取指定位置的元素。</li>
<li><code>indexOf(element)</code> 返回元素在链表中的索引。如果链表中没有该元素就返回 -1。</li>
<li><code>update(position, element)</code> 修改指定位置上的元素。</li>
<li><code>removeAt(position)</code> 从链表中的删除指定位置的元素。</li>
<li><code>remove(element)</code> 从链表删除指定的元素。</li>
<li><code>isEmpty()</code> 如果链表中不包含任何元素，返回 trun，如果链表长度大于 0 则返回 false。</li>
<li><code>size()</code> 返回链表包含的元素个数，与数组的 length 属性类似。</li>
<li><code>forwardString()</code> 返回正向遍历节点字符串形式。</li>
<li><code>backwordString()</code> 返回反向遍历的节点的字符串形式。</li>
</ul>
<h5 id="JavaScript实现双向链表"><a href="#JavaScript实现双向链表" class="headerlink" title="JavaScript实现双向链表"></a>JavaScript实现双向链表</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(data) &#123;</span><br><span class="line">        <span class="built_in">this</span>.data = data</span><br><span class="line">        <span class="built_in">this</span>.next = <span class="literal">null</span></span><br><span class="line">        <span class="built_in">this</span>.prev = <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkedList</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.length = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.head = <span class="literal">null</span></span><br><span class="line">        <span class="built_in">this</span>.tail = <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">    append(element) &#123;</span><br><span class="line">        <span class="keyword">let</span> newNode = <span class="keyword">new</span> Node(element)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.length === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">this</span>.head = newNode</span><br><span class="line">            <span class="built_in">this</span>.tail = newNode</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="built_in">this</span>.tail.next = newNode</span><br><span class="line">            newNode.prev = <span class="built_in">this</span>.tail</span><br><span class="line">            <span class="built_in">this</span>.tail = newNode</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.length++</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    insert(position, element) &#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt; <span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">let</span> newNode = <span class="keyword">new</span> Node(element)</span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">let</span> previousNode;</span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="built_in">this</span>.head === <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="built_in">this</span>.head = newNode</span><br><span class="line">                <span class="built_in">this</span>.tail = newNode</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                newNode.next = currentNode</span><br><span class="line">                currentNode.prev = newNode</span><br><span class="line">                <span class="built_in">this</span>.head = newNode</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (position === <span class="built_in">this</span>.length) &#123;</span><br><span class="line">            newNode.prev = <span class="built_in">this</span>.tail</span><br><span class="line">            <span class="built_in">this</span>.tail.next = newNode</span><br><span class="line">            <span class="built_in">this</span>.tail = newNode</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                previousNode = currentNode</span><br><span class="line">                currentNode = currentNode.next</span><br><span class="line">                position--</span><br><span class="line">            &#125;</span><br><span class="line">            newNode.prev = previousNode</span><br><span class="line">            newNode.next = currentNode</span><br><span class="line">            previousNode.next = newNode</span><br><span class="line">            currentNode.prev = newNode</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.length++</span><br><span class="line">        <span class="keyword">return</span> currentNode</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    get(position) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt;= <span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.head.data</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (position === <span class="built_in">this</span>.length - <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.tail.data</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            position--</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> currentNode.data</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    indexOf(element) &#123;</span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">        <span class="keyword">if</span> (!<span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="number">-1</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (currentNode) &#123;</span><br><span class="line">            <span class="keyword">if</span> (currentNode.data === element) &#123;</span><br><span class="line">                <span class="keyword">return</span> index</span><br><span class="line">            &#125;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            index++</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    update(position, element) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt;= <span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">this</span>.head.data = element</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            position--</span><br><span class="line">        &#125;</span><br><span class="line">        currentNode.data = element</span><br><span class="line">        <span class="keyword">return</span> currentNode</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    removeAt(position) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt;= <span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">let</span> prevNode;</span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="built_in">this</span>.length === <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="built_in">this</span>.head = <span class="literal">null</span></span><br><span class="line">                <span class="built_in">this</span>.tail = <span class="literal">null</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="built_in">this</span>.head = <span class="built_in">this</span>.head.next</span><br><span class="line">                <span class="built_in">this</span>.head.prev = <span class="literal">null</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (position === <span class="built_in">this</span>.length - <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="built_in">this</span>.tail = <span class="built_in">this</span>.tail.prev</span><br><span class="line">            <span class="built_in">this</span>.tail.next = <span class="literal">null</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                prevNode = currentNode</span><br><span class="line">                currentNode = currentNode.next</span><br><span class="line">                position--</span><br><span class="line">            &#125;</span><br><span class="line">            prevNode.next = currentNode.next</span><br><span class="line">            currentNode.next.prev = prevNode</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.length--</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    remove(element) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.removeAt(<span class="built_in">this</span>.indexOf(element))</span><br><span class="line">    &#125;</span><br><span class="line">    isEmpty() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length === <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    forwardString() &#123;</span><br><span class="line">        <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">while</span> (currentNode) &#123;</span><br><span class="line">            str += currentNode.data + <span class="string">&#x27; &#x27;</span></span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line">    &#125;</span><br><span class="line">    backwordString() &#123;</span><br><span class="line">        <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.tail</span><br><span class="line">        <span class="keyword">while</span> (currentNode) &#123;</span><br><span class="line">            str += currentNode.data + <span class="string">&#x27; &#x27;</span></span><br><span class="line">            currentNode = currentNode.prev</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> link = <span class="keyword">new</span> LinkedList()</span><br><span class="line">link.append(<span class="number">1</span>)</span><br><span class="line">link.append(<span class="number">2</span>)</span><br><span class="line">link.insert(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="comment">// 1 2 3</span></span><br><span class="line">link.update(<span class="number">2</span>, <span class="number">5</span>) <span class="comment">// 1 2 5</span></span><br><span class="line"><span class="built_in">console</span>.log(link.forwardString())   <span class="comment">// 1 2 5</span></span><br><span class="line"><span class="built_in">console</span>.log(link.backwordString())  <span class="comment">// 5 2 1</span></span><br></pre></td></tr></table></figure>
<h3 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h3><blockquote>
<p>集合比较常见的实现方式是哈希表，这里使用 <code>JavaScript</code> 的 <code>Object</code> 进行封装</p>
</blockquote>
<h4 id="集合特点"><a href="#集合特点" class="headerlink" title="集合特点"></a>集合特点</h4><ul>
<li>集合通常是由一组<code>无序的</code>、<code>不能重复</code>的元素构成</li>
<li>数学中常指的集合中的元素是可以重复的，但是计算机中集合的元素不能重复</li>
<li>集合是特殊的数组<ul>
<li>特殊之处在于里面的元素没有顺序，也不能重复</li>
<li>没有顺序意味着不能通过下标值进行访问，不能重复意味着相同的对象在集合中只会存在一份</li>
</ul>
</li>
</ul>
<h4 id="常用操作-5"><a href="#常用操作-5" class="headerlink" title="常用操作"></a>常用操作</h4><ul>
<li>add(value) </li>
<li>remove(value)</li>
<li>has(value)</li>
<li>clear() 移除所有项</li>
<li>size()</li>
<li>values() 返回一个包含集合中所有值的数组</li>
</ul>
<h4 id="集合之间的操作"><a href="#集合之间的操作" class="headerlink" title="集合之间的操作"></a>集合之间的操作</h4><ul>
<li>并集：对于给定的两个集合，返回一个包含两个集合中所有元素的新集合。</li>
<li>交集：对于给定的两个集合，返回一个包含两个集合中共有元素的新集合。</li>
<li>差集：对于给定的两个集合，返回一个包含所有存在于第一个集合且不存在于第二个集合的元素的新集合。</li>
<li>子集：验证一个给定集合是否是另一个集合的子集。<br><img src="https://user-images.githubusercontent.com/24516169/88532735-b1069a00-d037-11ea-9ece-e19b2b8a09e2.png"></li>
</ul>
<h4 id="JavaScript实现集合"><a href="#JavaScript实现集合" class="headerlink" title="JavaScript实现集合"></a>JavaScript实现集合</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Set</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    has(value) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.hasOwnProperty(value)</span><br><span class="line">    &#125;</span><br><span class="line">    add(value) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.has(value)) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="built_in">this</span>.items[value] = value</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">    remove(value) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.has(value)) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">delete</span> <span class="built_in">this</span>.items[value]</span><br><span class="line">    &#125;</span><br><span class="line">    clear() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Object</span>.keys(<span class="built_in">this</span>.items).length</span><br><span class="line">    &#125;</span><br><span class="line">    values() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Object</span>.values(<span class="built_in">this</span>.items)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 集合间的操作</span></span><br><span class="line"></span><br><span class="line">     <span class="comment">// union() 求两个集合的并集</span></span><br><span class="line">    union(other) &#123;</span><br><span class="line">        <span class="keyword">let</span> unionSet = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> <span class="built_in">this</span>.values()) &#123;</span><br><span class="line">            unionSet.add(value)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> other.values()) &#123;</span><br><span class="line">            unionSet.add(value)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> unionSet</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// intersection() 求两个集合的交集</span></span><br><span class="line">    intersection(other) &#123;</span><br><span class="line">        <span class="keyword">let</span> intersectionSet = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> <span class="built_in">this</span>.values()) &#123;</span><br><span class="line">            <span class="keyword">if</span> (other.has(value)) &#123;</span><br><span class="line">                intersectionSet.add(value)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> intersectionSet</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// difference() 差集</span></span><br><span class="line">    difference(other) &#123;</span><br><span class="line">        <span class="keyword">let</span> differenceSet = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> <span class="built_in">this</span>.values()) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!other.has(value)) &#123;</span><br><span class="line">                differenceSet.add(value)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> differenceSet</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// subset() 子集</span></span><br><span class="line">    subset(other) &#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">let</span> value <span class="keyword">of</span> <span class="built_in">this</span>.values())&#123;</span><br><span class="line">            <span class="keyword">if</span>(!other.has(value))&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> set = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line">set.add(<span class="string">&#x27;abc&#x27;</span>)</span><br><span class="line">set.add(<span class="string">&#x27;abc&#x27;</span>)</span><br><span class="line">set.add(<span class="string">&#x27;123&#x27;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(set) <span class="comment">// &#123;&#x27;abc&#x27;:&#x27;abc&#x27;,&#x27;123&#x27;:&#x27;123&#x27;&#125;</span></span><br></pre></td></tr></table></figure>

<h3 id="字典"><a href="#字典" class="headerlink" title="字典"></a>字典</h3><h4 id="特点"><a href="#特点" class="headerlink" title="特点"></a>特点</h4><ul>
<li>字典存储的是<code>键值对</code>，主要特点是<code>一一对应</code></li>
<li><code>key</code>是不能重复且无序的，而<code>Value</code>可以重复</li>
</ul>
<h4 id="常用操作-6"><a href="#常用操作-6" class="headerlink" title="常用操作"></a>常用操作</h4><ul>
<li><code>set(key,value)</code> 向字典中添加新元素</li>
<li><code>remove(key)</code> 通过使用键值来移除对应数据</li>
<li><code>has(key)</code></li>
<li><code>get(key)</code></li>
<li><code>clear()</code></li>
<li><code>size()</code></li>
<li><code>keys()</code></li>
<li><code>values()</code></li>
</ul>
<h4 id="JavaScript代码实现"><a href="#JavaScript代码实现" class="headerlink" title="JavaScript代码实现"></a>JavaScript代码实现</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Map</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    has(key) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.hasOwnProperty(key)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    set(key, value) &#123;</span><br><span class="line">        <span class="built_in">this</span>.items[key] = value</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    remove(key) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.has(key)) <span class="keyword">delete</span> <span class="built_in">this</span>.items[key]</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    get(key) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.has(key)) <span class="keyword">return</span> <span class="built_in">this</span>.items[key]</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">undefined</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    clear() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Object</span>.keys(<span class="built_in">this</span>.items).length</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    keys() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Object</span>.keys(<span class="built_in">this</span>.items)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    values() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Object</span>.values(<span class="built_in">this</span>.items)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="散列-哈希-表（Hash）"><a href="#散列-哈希-表（Hash）" class="headerlink" title="散列[哈希]表（Hash）"></a>散列[哈希]表（Hash）</h3><blockquote>
<p>哈希表的结构就是数组，但它神奇之处在于<code>对下标值的一种变换</code>，这种变换我们可以称之为<code>哈希函数</code>，通过哈希函数可以获取 <code>HashCode</code></p>
</blockquote>
<h3 id="树（Tree）"><a href="#树（Tree）" class="headerlink" title="树（Tree）"></a>树（Tree）</h3><h3 id="堆（Heap）"><a href="#堆（Heap）" class="headerlink" title="堆（Heap）"></a>堆（Heap）</h3><h2 id="算法"><a href="#算法" class="headerlink" title="算法"></a>算法</h2><blockquote>
<p>解决问题的方法/步骤逻辑</p>
</blockquote>
<h3 id="排序算法"><a href="#排序算法" class="headerlink" title="排序算法"></a>排序算法</h3><p><img src="https://i.bmp.ovh/imgs/2021/03/21390fb96e8a3e59.png"></p>
<h4 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a>冒泡排序</h4><ul>
<li>比较相邻的元素。如果第一个比第二个大，就交换他们两个。 </li>
<li>对每一对相邻元素做同样的工作，从开始第一对到结尾的最后一对。在这一点，最后的元素应该会是最大的数。</li>
<li>针对所有的元素重复以上的步骤，除了最后一个。</li>
<li>持续每次对越来越少的元素重复上面的步骤，直到没有任何一对数字需要比较。 </li>
</ul>
<p><img src="https://i.bmp.ovh/imgs/2021/03/616b7d6f783af0de.png"></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">bubbleSort</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> j = <span class="number">0</span>; j &lt; arr.length - i - <span class="number">1</span>; j++) &#123;</span><br><span class="line">            <span class="keyword">let</span> m = arr[j]</span><br><span class="line">            <span class="keyword">let</span> n = arr[j + <span class="number">1</span>]</span><br><span class="line">            <span class="keyword">if</span> (n &lt; m) [arr[j + <span class="number">1</span>], arr[j]] = [m, n]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a>快速排序</h4><blockquote>
<p>对冒泡排序的改进</p>
</blockquote>
<p>设要排序的数组是A[0]……A[N-1]，首先任意选取一个数据（通常选用数组的第一个数）作为关键数据，<br>然后将所有比它小的数都放到它左边，所有比它大的数都放到它右边，这个过程称为一趟快速排序。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">quickSort</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> array = arr</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">sort</span>(<span class="params">array</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">let</span> left = [], center, right = []</span><br><span class="line">        <span class="keyword">if</span> (array.length &lt;= <span class="number">1</span>) <span class="keyword">return</span> array</span><br><span class="line">        center = array[<span class="number">0</span>]</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">1</span>; i &lt; array.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (array[i] &gt; center) &#123;</span><br><span class="line">                right.push(array[i])</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                left.push(array[i])</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> [...sort(left), center, ...sort(right)]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> sort(array)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="选择排序"><a href="#选择排序" class="headerlink" title="选择排序"></a>选择排序</h4><blockquote>
<p>首先在未排序序列中找到最小（大）元素，存放到排序序列的起始位置，然后，再从剩余未排序元素中继续寻找最小（大）元素，然后放到已排序序列的末尾。以此类推，直到所有元素均排序完毕。</p>
</blockquote>
<p><img src="https://i.bmp.ovh/imgs/2021/03/1f1a3ce04f8fa45a.png"></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">searchSort</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        <span class="keyword">let</span> m = i</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> j = i + <span class="number">1</span>; j &lt; arr.length; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (arr[m] &gt; arr[j]) &#123;</span><br><span class="line">                m = j <span class="comment">// 找到最小值所在的位置</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 位置置换</span></span><br><span class="line">        [arr[m], arr[i]] = [arr[i], arr[m]]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="插入排序"><a href="#插入排序" class="headerlink" title="插入排序"></a>插入排序</h4><blockquote>
<p>在待排序的元素中，假设前面n-1(其中n&gt;=2)个数已经是排好顺序的，现将第n个数插到前面已经排好的序列中，然后找到合适自己的位置，使得插入第n个数的这个序列也是排好顺序的。按照此法对所有元素进行插入，直到整个序列排为有序的过程，称为插入排序</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">insertSort</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> j;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">1</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        <span class="keyword">var</span> temp = arr[i]</span><br><span class="line">        j = i - <span class="number">1</span></span><br><span class="line">        <span class="keyword">while</span> (temp &lt; arr[j] &amp;&amp; j &gt;= <span class="number">0</span>) &#123;</span><br><span class="line">            arr[j + <span class="number">1</span>] = arr[j]</span><br><span class="line">            j--</span><br><span class="line">        &#125;</span><br><span class="line">        arr[j + <span class="number">1</span>] = temp</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/11/24/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/30/dart/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Dart基础语法
          
        </div>
      </a>
    
    
      <a href="/2020/11/20/%E4%BB%A3%E7%A0%81/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">代码段</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "9OLyDF6ymKbs1woL9kh2qbae-gzGzoHsz",
    app_key: "RTkmORaJHqkGjEovHixLK04x",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2019-2023
        <i class="ri-heart-fill heart_icon"></i> John Doe
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.webp" alt="Doreen&#39;s Blog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->


<script src="/js/clickBoom2.js"></script>


<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>